<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@taglib prefix="shiro" uri="http://shiro.apache.org/tags" %>
<html>
<head>
    <title></title>
</head>
<body>
<div id="rolemanage_layout" class="easyui-layout" fit="true">
    <shiro:hasPermission name="permission:manage">
        <div data-options="region:'east',title:'权限',split:false,collapsible:false" style="width:400px;">
            <table id="rolemanage_permission_treegrid" style="width:100%;"></table>
            <div style="display: none;text-align: center;margin-top: 15px;" id="rolemanage_permission_grant_btn_div">
                <shiro:hasPermission name="role:grant">
                <button onclick="rolemanage.grantAuthen()" class="btn btn-sm btn-primary"><i
                        class="icon-plus-sign  align-top bigger-125"></i>授权
                </button>
                </shiro:hasPermission>
            </div>
        </div>
    </shiro:hasPermission>
    <div data-options="region:'center',title:'角色管理'">
        <table id="rolemanage_datagrid"></table>
        <div style="display: none;text-align: center;margin-top: 15px;" id="rolemanage_add_btn_div">
            <shiro:hasPermission name="role:add">
                <a href="${pageContext.request.contextPath}/admin/role/add.html" class="btn btn-sm btn-primary"><i
                        class="icon-plus-sign  align-top bigger-125"></i>添加角色</a>
            </shiro:hasPermission>
        </div>
    </div>
</div>
<script>
    var windowheight = $(window).height();
    windowheight = parseInt(windowheight);
    var bodyheight = $('body').height();
    bodyheight = parseInt(bodyheight);
    var $height = windowheight > bodyheight ? windowheight : bodyheight;
    $('#rolemanage_layout').css('height', parseInt($height) - 5 - (parseInt($("#navbar").height()) + parseInt($("#breadcrumbs").height())) + "px")
    top.setMenuActionOpen('系统设置', '角色管理');
    var rolemanage = {
        roleGridInit: function () {
            using("datagrid", function () {
                $('#rolemanage_datagrid').datagrid({
                    url: '${pageContext.request.contextPath}/admin/role/getRolesManageAjax.html',
                    fitColumns: true,
                    striped: true,
                    idField: "id",
                    rownumbers: true,
                    singleSelect: true,
                    columns: [[
                        {field: 'name', title: '角色名字', width: '40%'},
                        {field: 'description', title: '角色描述', width: '40%'},
                        {
                            field: 'action',
                            title: '操作',
                            hidden: true,
                            width: '18%',
                            formatter: function (value, row, index) {
                                var _actionbtn = "";
                                if (GlobalPermission.role_edit) {
                                    _actionbtn += "&nbsp;<a href='${pageContext.request.contextPath}/admin/role/edit/" + row.id + ".html'  title='编辑' class='btn btn-xs btn-info'><i class='icon-edit bigger-120'></i></a>&nbsp;";
                                }
                                if (GlobalPermission.role_del) {
                                    _actionbtn += "&nbsp;<button onclick='rolemanage.deleteRole(" + row.id + ")' title='删除' class='btn btn-xs btn-danger'><i class='icon-trash bigger-120'></i></button>&nbsp;";
                                }
                                return _actionbtn;
                            }
                        }
                    ]],
                    loadFilter: function (data) {
                        var error = data.error;
                        if (error) {
                            Common.error(error);
                            return;
                        }
                        return data.success;
                    },
                    onLoadError: function () {
                        LoadError();
                    },
                    onLoadSuccess: function (data) {
                        $("#rolemanage_add_btn_div").show();
                        if (GlobalPermission.role_edit||GlobalPermission.role_del) {
                            $('#rolemanage_datagrid').datagrid('showColumn', 'action');
                        }
                    },
                    onClickRow: function (index, row) {
                        if (GlobalPermission.role_permission){

                        rolemanage.queryAuthen(row.id);
                        }
                    }

                });
            });
        },
        permissionGridInit: function () {
            using("treegrid", function () {
                $('#rolemanage_permission_treegrid').treegrid({
                    url: '${pageContext.request.contextPath}/admin/permission/getManagePermissionsAjax.html',
                    idField: 'id',
                    treeField: 'name',
                    fitColumns: true,
                    striped: true,
                    method: 'post',
                    rownumbers: true,
                    singleSelect: false,
                    animate: true,
                    lines: true,
                    columns: [[
                        {field: 'id', checkbox: true},
                        {title: '权限名字', field: 'name', width: 100},
                        {field: 'description', title: '权限描述', width: 100}
                    ]],
                    loadFilter: function (data) {
                        var error = data.error;
                        if (error) {
                            Common.error(error);
                            return;
                        }
                        return data.success;
                    },
                    onLoadError: function () {
                        LoadError();
                    },
                    onCheck: function (node) {
                        var parentPermissions = $('#rolemanage_permission_treegrid').treegrid("getParent", node.id);
                        $(parentPermissions).each(function (i, v) {
                            $('#rolemanage_permission_treegrid').treegrid("select", v.id);
                        });
                    },
                    onUncheck: function (node) {
                        var childrenPermissions = $('#rolemanage_permission_treegrid').treegrid("getChildren", node.id);
                        $(childrenPermissions).each(function (i, v) {
                            $('#rolemanage_permission_treegrid').treegrid("unselect", v.id);
                        });
                    },
                    onLoadSuccess: function (data) {
                        $("#rolemanage_permission_grant_btn_div").show();
                    }
                });
            });
        },
        init: function () {
            rolemanage.roleGridInit();
            rolemanage.permissionGridInit();
        },
        deleteRole: function (id) {
            Common.confirm("你确定要删除所选角色吗？", function (r) {
                if (r) {
                    $.ajax({
                        url: '${pageContext.request.contextPath}/admin/role/deleteAjax.html',
                        data: {"id": id},
                        dataType: 'json',
                        success: function (data) {
                            var error = data.error;
                            if (error) {
                                Common.error(error);
                                return;
                            }
                            Common.info(data.success);
                            $('#rolemanage_datagrid').datagrid('load');
                        }
                    });
                }
            })
        },
        queryAuthen: function (id) {
            $.ajax({
                url: '${pageContext.request.contextPath}/admin/rolepermission/getRolePermissionsAjax.html',
                data: {"id": id},
                success: function (data) {
                    var error = data.error;
                    if (error) {
                        Common.error(error);
                        return;
                    }
                    $('#rolemanage_permission_treegrid').treegrid("unselectAll");
                    $(data.success).each(function (i, v) {
                        $('#rolemanage_permission_treegrid').treegrid("select", v);
                    });
                }
            });
        },
        grantAuthen: function () {
            var roleSelected = $('#rolemanage_datagrid').datagrid('getSelected');
            if (!roleSelected) {
                Common.notice("请选择角色分配权限")
                return;
            }
            var roleId = roleSelected.id;
            var perIds = new Array();
            var perSelected = $('#rolemanage_permission_treegrid').treegrid("getSelections");
            $(perSelected).each(function (i, v) {
                perIds.push(v.id);
            });
            if (perIds.length <= 0) {
                perIds.push(0);
            }
            Common.confirm("你确定要授权吗？", function (r) {
                if (r) {
                    $.ajax({
                        url: '${pageContext.request.contextPath}/admin/rolepermission/grantRolePermissionsAjax.html',
                        data: {"roleId": roleId, perIds: perIds},
                        success: function (data) {
                            var error = data.error;
                            if (error) {
                                Common.error(error);
                                return;
                            }
                            Common.info(data.success);
                        }
                    });
                }
            })
        }

    };
    rolemanage.init();
</script>
</body>
</html>
